package controllers;

import java.util.Calendar;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;

import org.joda.time.Days;

import net.sf.cglib.transform.impl.AddDelegateTransformer;

import com.sun.org.apache.bcel.internal.generic.NEW;

import models.Contract;
import models.Product;

import play.mvc.Controller;

public class Contracts extends Controller {

	static Date d1 = new Date();

	public static void index() {
		render();
	}

	public static void create(String name1, String name2, String address,
			long contract_id, Date date, String Product1, String Product2,
			String Product3, String Product4, String Product5, int quantity1,
			int quantity2, int quantity3, int quantity4, int quantity5,
			int no_of_change_per_month, int item1_price, int item2_price,
			int item3_price, int item4_price, int item5_price,
			int total1_price, int total2_price, int total3_price,
			int total4_price, int total5_price, String mobile1, String mobile2,
			String floor_type, String phone1, String phone2, String area,
			int year, String smell1, String smell2, String smell3,
			String smell4, String smell5, String floor_type1,
			String floor_type2, String floor_type3, String floor_type4,
			String floor_type5) {

		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<Product> pl = new ArrayList<Product>();
		boolean flag = false;

		for (int i = 0; i < allContracts.size(); i++) {
			if (!allContracts.get(i).mobile1.isEmpty()
					&& allContracts.get(i).mobile1.equalsIgnoreCase(mobile1))
				flag = true;
			if (!allContracts.get(i).mobile2.isEmpty()
					&& allContracts.get(i).mobile1.equalsIgnoreCase(mobile1))
				flag = true;
		}

		if (!Product1.equals("none")) {
			Product p1 = new Product(Product1, quantity1,
					no_of_change_per_month, item1_price, smell1,
					new LinkedList<Date>(), floor_type1);

			p1.addChangeTimes(p1, no_of_change_per_month, date);
			p1.save();
			pl.add(p1);

		}
		if (!Product2.equals("none")) {
			Product p2 = new Product(Product2, quantity2,
					no_of_change_per_month, item2_price, smell2,
					new LinkedList<Date>(), floor_type2);
			p2.addChangeTimes(p2, no_of_change_per_month, date);
			p2.save();
			pl.add(p2);

		}

		if (!Product3.equals("none")) {
			Product p3 = new Product(Product3, quantity3,
					no_of_change_per_month, item3_price, smell3,
					new LinkedList<Date>(), floor_type3);
			p3.addChangeTimes(p3, no_of_change_per_month, date);

			p3.save();

			pl.add(p3);
		}
		if (!Product4.equals("none")) {
			Product p4 = new Product(Product4, quantity4,
					no_of_change_per_month, item4_price, smell4,
					new LinkedList<Date>(), floor_type4);
			p4.addChangeTimes(p4, no_of_change_per_month, date);

			p4.save();
			pl.add(p4);

		}

		if (!Product5.equals("none")) {
			Product p5 = new Product(Product5, quantity5,
					no_of_change_per_month, item1_price, smell5,
					new LinkedList<Date>(), floor_type5);
			p5.addChangeTimes(p5, no_of_change_per_month, date);

			p5.save();

			pl.add(p5);

		}
		Contract c = new Contract(name1, name2, mobile1, mobile2, phone1,
				phone2, area, contract_id, year, date, address, floor_type, pl);
		for (int i = 0; i < pl.size(); i++) {
			c.plistNames.add(pl.get(i).name);
		}

		c.save();
		render(c, flag);

	}

	public static void showProducts(long id) {
		Contract C = Contract.findById(id);
		ArrayList<Product> plist = C.plist;
		render(plist);
	}

	public static void getAllContracts() {
		ArrayList<Contract> conList = (ArrayList) Contract.findAll();
		render(conList);
	}

	public static void edit(Long id) {
		Contract c = Contract.findById(id);
		// ArrayList<Product> p = c.plist;
		render(c, id);
	}
	public static void delete(Long id) {
		
	
		Contract c = Contract.findById(id);
		c.delete();
		getAllContracts();
		
	}

	public static void save(Long id, String name1, String name2,
			String mobile1, String mobile2, String phone1, String phone2,
			long contract_id, int year, Date date, String address, String area) {
		Contract c = Contract.findById(id);
		c.name1 = name1;
		c.name2 = name2;
		c.mobile1 = mobile1;
		c.mobile2 = mobile2;
		c.phone1 = phone1;
		c.phone2 = phone2;
		c.contract_id = contract_id;
		c.address = address;
		c.area = area;
		c.date = date;
		c.year = year;
		c.save();
		render();

	}
}
